/**
 * 投标报价单信息 - 编辑页面
 * @author liuwei
 * @date 2019/11/26
 */
define(function () {
    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            form: {
                GWGIORIU_NAME: null,
                GWGIORIG_NAME: null,
                GWGC022: null,
                GWGI003_NAME: null,
                GWGI008_NAME: null,
                GWGI009_NAME: null,
                GWGI010_NAME: null
            },
            selectOptions: {
                sys_n_y: app.datas_("sys_n_y"),
                gwga015Options: app.datas_("gwga015")
            },
            detailReadOnly: false,
            rules: {
                GWGI001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGC022: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGI002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGI004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGI005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGI006: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGI007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGI008: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGI009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            GWGI015_diabled: true
        },
        gwgjListData = [],
        gwgkListData = [],

        gwgj_cols = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWGJ002', width: 80, title: '序号'}
            , {field: 'GWGJ003', width: 100, title: '标段号'}
            , {field: 'GWGJ005', width: 120, title: '产品编号'}
            , {field: 'GWAHL003', width: 80, title: '产品大类'}
            , {field: 'GWAOL003', width: 80, title: '车辆类型'}
            , {field: 'GWGJ006', width: 100, title: '数量'}
            , {field: 'GWGJ007', width: 100, title: '预算单价(元)'}
            , {field: 'GWGJ008', width: 130, title: '区域建议报价(元)', edit: 'text'}
            // , {field: 'GWGJ009', width: 100, title: '投标处建议报价', edit: 'text'}
            // , {field: 'GWGJ010', width: 100, title: '最终定价', edit: 'text'}
            , {field: 'GWGJ012', width: 100, title: '有无购置税', edit: 'select', filterDatas: app.datas_("gwgk012")}
            , {field: 'GWGJ011', width: 100, title: '购置税(元)'}

        ]],
        gwgk_cols = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWGK002', width: 80, title: '序号'}
            , {field: 'GWGK003', width: 120, title: '项目', edit: 'text'}
            , {field: 'GWGK004', width: 120, title: '主要评分条款', edit: 'text'}
            , {field: 'GWGK006', width: 120, title: '自评情况', edit: 'text'}
            , {field: 'GWGK007', width: 120, title: '竞争厂商1评分', edit: 'text'}
            , {field: 'GWGK008', width: 120, title: '竞争厂商2评分', edit: 'text'}
            , {field: 'GWGK009', width: 120, title: '竞争厂商3评分', edit: 'text'}
            , {field: 'GWGK010', width: 120, title: '竞争厂商4评分', edit: 'text'}
            , {field: 'GWGK011', width: 120, title: '竞争厂商5评分', edit: 'text'}
        ]],
        callObj,
        pageVue,
        digit = {
            price: 100,
            amount: 100
        },
        table, form,
        gwgjEditTable, gwgkEditTable,
        layOpenIndex
    ;

    var Gwai305Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwgi005_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initGwgjTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                form = layui.form;
                gwgjEditTable = table.render({
                    elem: app.getByid(doc, 'gwgi005_gwgj_table'),
                    id: 'gwgi005_gwgj_table',
                    height: '240',
                    cols: gwgj_cols,
                    data: gwgjListData,
                    done: function (res, curr, count) {

                    }
                });

            });

            table.on('edit(gwgi005_gwgj_table)', function (obj) {
                var oldValue = $(this).prev().text();
                if (obj.field === 'GWGJ008') { // 中标单价
                    self.onGwgj008Change(obj, oldValue);
                } else if (obj.field === 'GWGJ012') {
                    self.onGwgj012Change(obj, oldValue)
                }
                // else if (obj.field === 'GWGJ009') {
                //     self.onGwgj009Change(obj,oldValue)
                // }else if (obj.field === 'GWGJ010') {
                //     self.onGwgj010Change(obj,oldValue)
                // }
                obj.updateOfEdit({
                    GWGJ008: obj.data.GWGJ008
                    // GWGJ009: obj.data.GWGJ009,
                    // GWGJ010: obj.data.GWGJ010,
                });
            });
        },
        gwgjTableReload: function (listData) {
            self._reloadTable({id: 'gwgi005_gwgj_table', data: listData});
        },

        initGwgkTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                form = layui.form;
                gwgkEditTable = table.render({
                    elem: app.getByid(doc, 'gwgi005_gwgk_table'),
                    id: 'gwgi005_gwgk_table',
                    height: '240',
                    cols: gwgk_cols,
                    data: gwgkListData,
                    done: function (res, curr, count) {

                    }
                });
            });

            // table.on('edit(gwgi005_gwgk_table)', function (obj) {
            //     let oldValue = $(this).prev().text();
            //     let data = obj.data;
            //     if (obj.field === 'GWGK006') {
            //         var re = /^[0-9]*$/; //判断是否为数字类型
            //         if((!re.test(obj.value))){
            //             data.GWGK006 = oldValue;
            //             obj.updateOfEdit({
            //                 GWGK006: oldValue
            //             });
            //             app.showWarning("自评得分必须为数字类型");
            //             return;
            //         }
            //     }
            // });
        },
        gwgkTableReload: function (listData) {
            self._reloadTable({id: 'gwgi005_gwgk_table', data: listData});
            if (listData.length > 0) {
                var tds = $('div[lay-id="gwgi005_gwgk_table"] tr').eq(1).children();
                for (let i = 2; i < 5; i++) {
                    tds.eq(i).removeAttr('data-edit');
                }
            }
        },

        numFormatUtil: function (num, digit) {
            return math.div(Math.round(math.mul(num, digit)), digit);
        },

        getDigit: function () {
            app.ajax({
                xurl: 'admin/getDigit',
                async: true,
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        var _digit = {
                            price: Math.pow(10, data.price),
                            amount: Math.pow(10, data.amount),

                        };
                        app.extendUpLow({
                            target: digit,
                            res: _digit,
                            reset: true
                        });
                        console.log(_digit)
                    } else {
                        console.log("获取小数位失败");
                    }
                }
            });
        },

        onGwgj008Change: function (obj, oldValue) {  // 区域建议报价改变
            if (obj.value === "") return;
            let data = obj.data;
            var re = /^-?\d+\.?\d{0,4}$/; //判断是否最多4位小数的数字类型
            if ((!re.test(obj.value)) || parseFloat(obj.value) < 0) {
                data.GWGJ008 = oldValue;
                obj.updateOfEdit({
                    GWGJ008: oldValue
                });
                app.showWarning("区域建议报价应为数字且小数位数最多4位");
                return;
            }
            if (obj.data.GWGJ012 === 'Y') {
                obj.data.GWGJ011 = self.numFormatUtil(math.div(self.getGwgi019(), 11.3), digit.amount);
                obj.updateOfEdit({
                    GWGJ011: obj.data.GWGJ011
                });
            }else {
                obj.updateOfEdit({
                    GWGJ011: 0
                });
            }
            vueData.form.GWGI016 = self.numFormatUtil(self.getGwgi016(), digit.amount);
            vueData.form.GWGI019 = self.numFormatUtil(self.getGwgi019(), digit.amount);
        },

        onGwgj012Change: function (obj, oldValue) {  // 有无购置税发生改变
            if (obj.data.GWGJ012 === 'Y') {
                obj.data.GWGJ011 = self.numFormatUtil(math.div(self.getGwgi019(), 11.3), digit.amount);
            } else {
                obj.data.GWGJ011 = 0;
            }
            obj.updateOfEdit({
                GWGJ012: obj.data.GWGJ012,
                GWGJ011: obj.data.GWGJ011
            });
        },

        // onGwgj009Change: function(obj,oldValue) {
        //     if(obj.value === "") return;
        //     let data = obj.data;
        //     var re = /^-?\d+\.?\d{0,4}$/; //判断是否最多4位小数的数字类型
        //     if((!re.test(obj.value)) || parseFloat(obj.value) < 0 ){
        //         data.GWGJ009 = oldValue;
        //         obj.updateOfEdit({
        //             GWGJ009: oldValue
        //         });
        //         app.showWarning("投标处建议报价应为数字且小数位数最多4位");
        //         return;
        //     }
        // },
        //
        // onGwgj010Change: function(obj,oldValue) { // 最终定价
        //     if(obj.value === "") return;
        //     let data = obj.data;
        //     var re = /^-?\d+\.?\d{0,4}$/; //判断是否最多4位小数的数字类型
        //     if((!re.test(obj.value)) || parseFloat(obj.value) < 0 ){
        //         data.GWGJ010 = oldValue;
        //         obj.updateOfEdit({
        //             GWGJ010: oldValue
        //         });
        //         app.showWarning("投标处建议报价应为数字且小数位数最多4位");
        //         return;
        //     }
        //     vueData.form.GWGI019 = self.numFormatUtil(self.getGwgi019(),digit.amount);
        // },

        // 汇总区域报价，计算中标服务费
        getGwgi016: function () {
            let listData = gwgjEditTable.getDatas();
            let gwgi016Sum = 0;
            listData.forEach(data => {
                let GWGJ006 = self.strToNum(data.GWGJ006);
                let GWGJ008 = self.strToNum(data.GWGJ008);
                let sum = math.mul(GWGJ006, GWGJ008);
                gwgi016Sum += sum;
            });
            switch (true) {
                case gwgi016Sum < math.mul(100 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.015);
                    break;
                case gwgi016Sum < math.mul(500 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.011);
                    break;
                case gwgi016Sum < math.mul(1000 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.008);
                    break;
                case gwgi016Sum < math.mul(5000 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.005);
                    break;
                case gwgi016Sum < math.mul(10000 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.025);
                    break;
                case gwgi016Sum < math.mul(100000 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.005);
                    break;
                case gwgi016Sum > math.mul(100000 , 10000):
                    gwgi016Sum = math.mul(gwgi016Sum, 0.001);
                    break;
            }
            return gwgi016Sum;
        },

        //汇总区域报价，计算总报价
        getGwgi019: function () {
            let listData = gwgjEditTable.getDatas();
            let gwgi019Sum = 0;
            listData.forEach(data => {
                let GWGJ006 = self.strToNum(data.GWGJ006);
                let GWGJ008 = self.strToNum(data.GWGJ008);
                let sum = math.mul(GWGJ006, GWGJ008);
                gwgi019Sum += sum;
            });
            return gwgi019Sum;
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwgi005/add',
                xdata: {
                    gwgiT: vueData.form,
                    gwgjList: gwgjListData,
                    gwgkList: gwgkListData
                },
                loading: true,
                toJsonStr: true,
                okmsg: '投标报价单信息增加成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwgi005').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwgi005/edit',
                xdata: {
                    gwgiT: vueData.form,
                    gwgjList: gwgjListData,
                    gwgkList: gwgkListData
                },
                loading: true,
                toJsonStr: true,
                okmsg: '投标报价单信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwgi005').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs.form.resetFields();
            layOpenIndex = tagData.layOpenIndex;
            gwgjListData = [];
            gwgkListData = [];
            if (tagData.is_add) {
                vueData.detailReadOnly = false;
                vueData.isAdd = true;
                // 获取单头新增的默认数据
                app.ajax({
                    xurl: 'crm/gwgi005/getAddData',
                    cb: function (result) {
                        if (result.ok) {
                            var resData = result.data.data;
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWGIORIU: resData.CURRENT_USER_ID,
                                    GWGIORIU_NAME: resData.CURRENT_USER_NAME,
                                    GWGIORIG: resData.CURRENT_ORG,
                                    GWGIORIG_NAME: resData.CURRENT_ORG_NAME,
                                    GWGIACTI: "N"
                                },
                                reset: true
                            });
                        }
                    }
                });

                self.gwgjTableReload(gwgjListData);
                self.gwgkTableReload(gwgkListData);
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                vueData.detailReadOnly = tagData.detailReadOnly;
                if (callObj.data.GWGIACTI !== 'N') {
                    vueData.detailReadOnly = true;
                }

                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });

                app.ajax({
                    xurl: 'crm/gwgi005/getDetailData',
                    xdata: {GWGIID: callObj.data.GWGIID},
                    loading: true,
                    cb: function (result) {
                        if (result.ok) {
                            let data = result.data.data;
                            gwgjListData = data.gwgjList;
                            gwgkListData = data.gwgkList;
                            self.gwgjTableReload(gwgjListData);
                            self.gwgkTableReload(gwgkListData);
                        }
                    }
                });
            }
        },

        strToNum: function (str) {
            if (str === undefined || str == null || str === '') {
                return 0;
            } else {
                return parseFloat(str);
            }
        },

        _ready: function () {
            table = layui.table;
            vueData.form = app.fields_(['GWGI_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwgi005_edit',
                data: vueData,
                methods: {
                    cancle: function () {
                        self._closeTpl();
                    },
                    saveData: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                let canSave = true;
                                for(let i = 1;i<gwgkListData.length;i++) {
                                    let gwgkData = gwgkListData[i];
                                    let GWGK003 = gwgkData.GWGK003;
                                    let GWGK004 = gwgkData.GWGK004;
                                    let GWGK006 = gwgkData.GWGK006;
                                    if(GWGK003 == null || GWGK003 === "" ||
                                       GWGK004 == null || GWGK004 === "" ||
                                       GWGK006 == null || GWGK006 === "" ) {
                                        canSave = false;
                                        break;
                                    }

                                }
                                if(canSave) {
                                    if (vueData.isAdd) {
                                        self.addData();
                                    } else {
                                        self.editData();
                                    }
                                }else {
                                    app.showWarning("项目，主要评分条款以及自评情况不可为空");
                                }
                            }
                        });
                    },
                    openGwgi001: function () {
                        let _self = this;
                        if (gwgjEditTable.getDatas().length > 0) {
                            app.confirm({
                                msg: "修改招标编号将清空报价明细，确定要修改吗？",
                                ok: function () {
                                    _self.changeGwgi001();
                                }
                            });
                        } else {
                            _self.changeGwgi001();
                        }
                    },

                    changeGwgi001: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwgc001_1 ',
                                    columnList: [
                                        {label: '招标投标编号', prop: 'GWGCID', width: '150'},
                                        {label: '招标编号', prop: 'GWGC001', width: '150'},
                                        {label: '项目名称', prop: 'GWGC022', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGIID = data.GWGCID;
                                    vueData.form.GWGI001 = data.GWGC001;
                                    vueData.form.GWGC022 = data.GWGC022;
                                    vueData.form.GWGI002 = data.GWGC024;
                                    vueData.form.GWGI003 = data.GWGC005;
                                    vueData.form.GWGI003_NAME = data.GWGC005_NAME;

                                    gwgjListData = [];
                                    gwgkListData = [];

                                    self.gwgjTableReload(gwgjListData);
                                    self.gwgkTableReload(gwgkListData);
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择投标信息',
                                area: ['600px', '465px']
                            }
                        });
                    },

                    // 投标报价明细表格操作
                    addGwgjData: function () {
                        if (vueData.form.GWGI001 == null || vueData.form.GWGI001 === "") {
                            app.showWarning("请先选择招标编号");
                            return;
                        }
                        app.ajax({
                            xurl: 'crm/gwgi005/getGwgdData',
                            xdata: {
                                GWGDID: vueData.form.GWGIID
                            },
                            loading: true,
                            cb: function (result) {
                                if (result.ok) {
                                    let gwgdListData = result.data.data;
                                    gwgjListData.forEach(gwgj => {
                                        gwgdListData.removeByMap({
                                            GWGD006: gwgj.GWGJ005
                                        })
                                    });

                                    let count = gwgjListData.length;
                                    // 投标明细数据
                                    gwgdListData.forEach(gwgd => {
                                        count = count + 1;
                                        gwgjListData.push({
                                            GWGJ002: count,
                                            GWGJ003: gwgd.GWGD003,
                                            GWGJ005: gwgd.GWGD006,
                                            GWAML004: gwgd.GWAML004,
                                            GWAHL003: gwgd.GWAHL003,
                                            GWAOL003: gwgd.GWAOL003,
                                            GWGJ006: gwgd.GWGD007,
                                            GWGJ007: gwgd.GWGD008 * 10000,
                                            GWGJ008: '',
                                            GWGJ009: '',
                                            GWGJ010: '',
                                            GWGJ011: 0,
                                            GWGJ012: 'N',
                                        })
                                    });

                                    self.gwgjTableReload(gwgjListData);
                                }
                            }
                        });

                    },
                    delGwgjData: function () {
                        var checkStatus = table.checkStatus('gwgi005_gwgj_table');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                gwgjListData.removeByMap({
                                    GWGJ002: v.GWGJ002
                                });
                            });
                            if (gwgjListData.length !== 0) {
                                let i = 1;
                                gwgjListData.forEach(function (v) {
                                    v.GWGJ002 = i;
                                    i++;
                                });
                            }
                            self.gwgjTableReload(gwgjListData);
                            vueData.form.GWGI016 = self.getGwgi016();
                            vueData.form.GWGI019 = self.getGwgi019();
                        } else {
                            app.showWarning("至少选择一笔数据进行操作")
                        }
                    },

                    // 投标评分预测表格操作
                    addGwgkData: function () {
                        let count = 0;
                        if (gwgkListData.length > 0) {
                            count = gwgkListData.length;
                        } else {
                            gwgkListData.push({
                                GWGK002: 0,
                                GWGK003: '',
                                GWGK004: '',
                                GWGK005: '',
                                GWGK006: '',
                                GWGK007: '',
                                GWGK008: '',
                                GWGK009: '',
                                GWGK010: '',
                                GWGK011: '',
                            });
                            count = 1;
                        }
                        gwgkListData.push({
                            GWGK002: count,
                            GWGK003: '',
                            GWGK004: '',
                            GWGK005: '',
                            GWGK006: '',
                            GWGK007: '',
                            GWGK008: '',
                            GWGK009: '',
                            GWGK010: '',
                            GWGK011: '',
                        });

                        self.gwgkTableReload(gwgkListData);
                    },
                    delGwgkData: function () {
                        gwgkListData = gwgkEditTable.getDatas();
                        var checkStatus = table.checkStatus('gwgi005_gwgk_table');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                gwgkListData.removeByMap({
                                    GWGK002: v.GWGK002
                                });
                            });
                            if (gwgkListData.length !== 0) {
                                let i = 1;
                                gwgkListData.forEach(function (v) {
                                    v.GWGK002 = i;
                                    i++;
                                });
                            }
                            self.gwgkTableReload(gwgkListData);
                        } else {
                            app.showWarning("至少选择一笔数据进行操作")
                        }
                    },

                    // 费用合计
                    custTotal: function () {
                        let total = self.strToNum(vueData.form.GWGI011) +
                            self.strToNum(vueData.form.GWGI012) +
                            self.strToNum(vueData.form.GWGI013) +
                            self.strToNum(vueData.form.GWGI014) +
                            self.strToNum(vueData.form.GWGI015) +
                            self.strToNum(vueData.form.GWGI016) +
                            self.strToNum(vueData.form.GWGI017);
                        let formatTotal = self.numFormatUtil(total, digit.amount);
                        vueData.form.GWGI018 = formatTotal;
                        return formatTotal;
                    }
                },
                watch: {
                    'form.GWGI011': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) < 0) {
                            app.showWarning("金额应为数字类型且小数位数最多为2位！");
                            vueData.form.GWGI011 = '';
                        }
                    },
                    'form.GWGI012': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) < 0) {
                            app.showWarning("金额应为数字类型且小数位数最多为2位！");
                            vueData.form.GWGI012 = '';
                        }
                    },
                    'form.GWGI013': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) < 0) {
                            app.showWarning("金额应为数字类型且小数位数最多为2位！");
                            vueData.form.GWGI013 = '';
                        }
                    },
                    'form.GWGI014': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) < 0) {
                            app.showWarning("金额应为数字类型且小数位数最多为2位！");
                            vueData.form.GWGI014 = '';
                        }
                    },
                    'form.GWGI015': function (newValue, oldValue) {  // 金额
                        if (newValue == null) return;
                        var re = /^-?\d+\.?\d{0,2}$/; //判断是否最多2位小数的数字类型
                        if ((!re.test(newValue)) || parseFloat(newValue) < 0) {
                            app.showWarning("金额应为数字类型且小数位数最多为2位！");
                            vueData.form.GWGI015 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwgi005_edit');
                }
            });
            self.initGwgjTable();
            self.initGwgkTable();
        },
        _lazy: function () {
            this._super();
            this.getDigit();
        }
    });

    return new Gwai305Edit();

})
;